﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using Microsoft.ApplicationBlocks.Data;

using YouHP.IDAL;
using YouHP.Model;

namespace YouHP.SqlServerDAL
{
    public class NewsInfoDAL: INewsInfo
    {
        #region INewsInfo 成员

        public bool AddNewsBulletin(NewsInfo newsInfo)
        {
            return SqlHelper.ExecuteNonQuery(Common.ConnStr,
                Common.NewsSP_AddNewsInfo,
                newsInfo.IsNews,
                newsInfo.Title,
                newsInfo.Brif,
                newsInfo.Body,
                newsInfo.ImagePath,
                newsInfo.CreateTime,
                newsInfo.LastUpdateTime
                ) > 0;
        }

        public bool DeleteNewsBulletin(int Id)
        {
            return SqlHelper.ExecuteNonQuery(Common.ConnStr,
                Common.NewsSP_DelNewsInfo,
                "Id = "+Id+""
                ) > 0;
        }

        public NewsInfo GetNewsBulletinById(int Id)
        {
            IDataReader reader = SqlHelper.ExecuteReader(Common.ConnStr,
                Common.NewsSP_GetNewsInfo,
                Id) as IDataReader;
            return CBO.FillObject<NewsInfo>(reader);
        }

        public bool UpdateNewsBulletin(NewsInfo newsInfo)
        {
            return SqlHelper.ExecuteNonQuery(Common.ConnStr,
                Common.NewsSP_UpdateNewsInfo,
                newsInfo.Id,
                newsInfo.IsNews,
                newsInfo.Title,
                newsInfo.Brif,
                newsInfo.Body,
                newsInfo.ImagePath,
                newsInfo.CreateTime,
                newsInfo.LastUpdateTime
                ) > 0;
        }

        #endregion

        public List<NewsInfo> GetNewsList()
        {
            DataSet dset = SqlHelper.ExecuteDataset(Common.ConnStr,
                           Common.NewsSP_GetNewsInfoList
                           );

            IDataReader dr = dset.Tables[0].CreateDataReader();
            if (dr != null && dr.Read())
            {
                //count = dr.GetInt32(0);
                dr.Close();
            }
            else
            {
                //count = 0;
            }
            dr = dset.Tables[0].CreateDataReader();

            return CBO.FillCollection<NewsInfo>(dr);
        }
        public List<NewsInfo> GetBullentinList()
        {
            DataSet dset = SqlHelper.ExecuteDataset(Common.ConnStr,
                           Common.NewsSP_GetBulletinList
                           );

            IDataReader dr = dset.Tables[0].CreateDataReader();
            if (dr != null && dr.Read())
            {
                //count = dr.GetInt32(0);
                dr.Close();
            }
            else
            {
                //count = 0;
            }
            dr = dset.Tables[0].CreateDataReader();

            return CBO.FillCollection<NewsInfo>(dr);
        }

                /*新闻分页检索, 输出符合条件的总新闻条数*/
        public List<NewsInfo> GetNewsInfoList(int pageSize, int pageIndex, string condStr, string orderStr, out int count)
        {
            DataSet ds = SqlHelper.ExecuteDataset(Common.ConnStr,
                Common.NewsSP_GetPagedNewsInfoList,
                pageSize,
                pageIndex,
                condStr,
                orderStr);

            IDataReader dr = ds.Tables[0].CreateDataReader();
            if (dr != null && dr.Read())
            {
                count = dr.GetInt32(0);
                dr.Close();
            }
            else
            {
                count = 0;
            }
            dr = ds.Tables[1].CreateDataReader();

            return CBO.FillCollection<NewsInfo>(dr);
        }
    }
}
